package cn.com.dhc.danlu.activity.envelope;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.DefaultFragmentActivity;
import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.dhc.danlu.widget.DLToolbar;
import cn.com.dhc.danlu.widget.PagerSlidingTabStrip;
import cn.com.dhc.danlu.widget.adapter.EnvelopeFragmentAdapter;
import cn.com.hd.mall.web.webservices.entity.request.envelop.GetMyCouponCntRequest;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetMyCouponCntResponse;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * @author wuhaiyang
 */
public class DanluEnvelopeActivity extends DefaultFragmentActivity /*implements CountCallback*/ {
    //    private DanluTaskProxy danluTaskProxy;
    private ViewPager mViewPager;
    private PagerSlidingTabStrip mTabStrip;
    private DLToolbar mToolbar;

    //    @Override
    //    public DanluTaskProxy getDanluTaskProxy() {
    //        return danluTaskProxy;
    //    }

    @Override
    protected void onCreate(Bundle saveInstance) {
        super.onCreate(saveInstance);
        setContentView(R.layout.activity_danlu_envelope);
        initView();
        EnvelopeFragmentAdapter pagerAdapter = new EnvelopeFragmentAdapter(getSupportFragmentManager(), EnvelopeFragmentAdapter.TYPE_ENVELOP_STATUS);
        mViewPager.setAdapter(pagerAdapter);
        mTabStrip.setViewPager(mViewPager);
        mToolbar.setRightClickListenter(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到说明界面
                startActivity(new Intent(DanluEnvelopeActivity.this, InstructionAndRuleActivity.class));
            }
        });
        mViewPager.setOffscreenPageLimit(3);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getEnvelopCount();
    }

    public void getEnvelopCount() {
        GetMyCouponCntRequest request = new GetMyCouponCntRequest();
        request.setToken(agent.getToken());
        request.setCompanyId(agent.getLoginUser().getCompanyId());
        agent.getApi()
                .getEnvelopCount(request)
                .compose(OperatorUtils.<GetMyCouponCntResponse>defaultSchedulers())
                .subscribe(new GeneralBusinessObserver<GetMyCouponCntResponse>() {
                    @Override
                    protected void onSuccess(GetMyCouponCntResponse model) {
                        if (!TextUtils.isEmpty(model.getUnUsedCnt())) {
                            onCountReturn(0, model.getUnUsedCnt());
                        }
                        if (!TextUtils.isEmpty(model.getUsedCnt())) {
                            onCountReturn(1, model.getUsedCnt());
                        }
                        if (!TextUtils.isEmpty(model.getExpireCnt())) {
                            onCountReturn(2, model.getExpireCnt());
                        }
                    }
                });
    }

    public void onCountReturn(int status, String count) {
        if (status == 0) {
            // 未使用
            mTabStrip.getTabByIndex(status).setText("未使用  (" + count + ")");
        } else if (status == 1) {
            // 已过期
            mTabStrip.getTabByIndex(status).setText("已使用  (" + count + ")");
        } else {
            // 已使用
            mTabStrip.getTabByIndex(status).setText("已过期  (" + count + ")");
        }
    }

    //    @Override
    //    public AsyncTaskActivityDelegate getDelegate() {
    //        return delegate;
    //    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        mToolbar = (DLToolbar) findViewById(R.id.toolbar);
    }
}